package com.gzhryc.shared_device.oem.h5.agent.mng.controllers.cdcz;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.cdcz.code.CDCZCostConfig;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZFeeTemplate;
import com.gzhryc.shared_device.cdcz.code.dao.models.CDCZPowerIntervalMoneyContent;
import com.gzhryc.shared_device.cdcz.code.services.CDCZFeeTemplateService;
import com.gzhryc.shared_device.oem.code.ConfigUtils;
import com.gzhryc.shared_device.oem.code.device.EDeviceFunCode;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceBusinessConfig;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.PlaceBusinessConfigService;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;
import com.gzhryc.shared_device.oem.h5.agent.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;

public class CDCZDeviceCostController {

    @Route(value = { "/cdcz/place/cost.html" })
    public String placeCost(HttpServletRequest request) {
        Long placeId = WebUtils.getLong(request,"placeId");
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(placeId);
            if (place != null && place.getUserId().equals(loginUser.getId())) {
                CDCZFeeTemplate feeTemplate = null;
                CDCZPowerIntervalMoneyContent content = null;
                PlaceBusinessConfig deviceFunc = PlaceBusinessConfigService.self().getByKeys(placeId, EDeviceFunCode.CDCZ.index());
                if (deviceFunc != null && deviceFunc.getFeeTemplateId() != null) {
                    feeTemplate = CDCZFeeTemplateService.self().getJoinById(deviceFunc.getFeeTemplateId());
                }

                if (feeTemplate != null) {
                    WebUtils.setAttribute(request, feeTemplate);
                    if (StringTools.isNotBlank(feeTemplate.getPimContent())) {
                        content = JsonTools.fromJson(feeTemplate.getPimContent(), CDCZPowerIntervalMoneyContent.class);
                    } else {
                        CDCZCostConfig costConfig = ConfigUtils.getCDCZCostConfig();
                        if (StringTools.isNotBlank(costConfig.getPimContent())) {
                            content = JsonTools.fromJson(costConfig.getPimContent(), CDCZPowerIntervalMoneyContent.class);
                        }
                    }
                } else {
                    CDCZCostConfig costConfig = ConfigUtils.getCDCZCostConfig();
                    feeTemplate = CDCZFeeTemplateService.buildByDefault(costConfig);
                    WebUtils.setAttribute(request, feeTemplate);

                    if (StringTools.isNotBlank(costConfig.getPimContent())) {
                        content = JsonTools.fromJson(costConfig.getPimContent(), CDCZPowerIntervalMoneyContent.class);
                    }
                }

                if (content != null && content.getItems() != null && content.getItems().size() > 0) {
                    request.setAttribute("powerIntervalList", content.getItems());
                }
            }
        }
        return "/cdcz/cost/place/config.html";
    }

    @Route(value = { "/cdcz/place/cost/change.do" })
    public AjaxResult doChange(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "保存失败");
        Long placeId = WebUtils.getLong(request,"placeId");
        Long feeTemplateId = WebUtils.getLong(request,"feeTemplateId");
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(placeId);
            if (place != null && place.getUserId().equals(loginUser.getId())) {
                try {
                    if (PlaceBusinessConfigService.self().setFeeTemplateId(placeId, EDeviceFunCode.CDCZ.index(), feeTemplateId)) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                } catch (LogicException e) {
                    result.setMsg(e.getMessage());
                }
            }else {
                result.setMsg("无权限操作");
            }
        }
        return result;
    }
}
